package com.iknow.activity;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.ComponentName;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.content.ServiceConnection;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.AsyncTask.Status;
import android.os.Bundle;
import android.os.IBinder;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.MenuItem.OnMenuItemClickListener;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnCreateContextMenuListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.Toast;
import com.iknow.IKnow;
import com.iknow.data.QingBo;
import com.iknow.db.IKnowDataBase;
import com.iknow.db.IKnowDatabaseHelper;
import com.iknow.task.CommonTask.DataMigration;
import com.iknow.task.CommonTask.ProductFavoriteTask;
import com.iknow.task.GenericTask;
import com.iknow.task.TaskAdapter;
import com.iknow.task.TaskListener;
import com.iknow.task.TaskParams;
import com.iknow.task.TaskResult;
import com.iknow.ui.model.FavoriteQingboAdapter;
import com.iknow.util.MsgDialog;
import com.iknow.view.widget.MyListView;
import com.iknow.xmpp.service.aidl.IXmppFacade;
import com.iknow.xmpp.service.aidl.IXmppFacade.Stub;
import java.util.ArrayList;
import java.util.List;

public class MyFavoriteActivity extends Activity
{
  private static final Intent SERVICE_INTENT;
  public static MyFavoriteActivity mfa = null;
  private DialogInterface.OnClickListener BuckupDialogClickListener;
  private DialogInterface.OnClickListener RestoreDialogClickListener;
  public View.OnCreateContextMenuListener listContextMenuListener;
  private AdapterView.OnItemClickListener listItemClickListener;
  private AdapterView.OnItemLongClickListener listItemLongClickListener;
  private FavoriteQingboAdapter mAdapter;
  private boolean mBinded;
  private Context mContext;
  private CommonTask.ProductFavoriteTask mFavoriteTask;
  private MyListView mList;
  private ServiceConnection mSvrConn;
  private TaskListener mTaskListener;
  private IXmppFacade mXmppFacade;
  private int positionView;

  static
  {
    SERVICE_INTENT = new Intent();
    SERVICE_INTENT.setComponent(new ComponentName("com.iknow", "com.iknow.xmpp.service.IKnowXmppService"));
  }

  public MyFavoriteActivity()
  {
    this.mSvrConn = new XmppServiceConnection(this, null);
    this.mAdapter = null;
    this.positionView = -1;
    this.listItemClickListener = new AdapterView.OnItemClickListener(this)
    {
      public void onItemClick(, View paramView, int paramInt, long paramLong)
      {
        QingBo localQingBo = MyFavoriteActivity.access$0(this.this$0).getItem(paramInt);
        if (localQingBo == null);
        while (true)
        {
          return;
          Intent localIntent = new Intent(MyFavoriteActivity.access$1(this.this$0), PageActivity.class);
          localIntent.putExtra("item", localQingBo);
          this.this$0.startActivity(localIntent);
        }
      }
    };
    this.listItemLongClickListener = new AdapterView.OnItemLongClickListener(this)
    {
      public boolean onItemLongClick(, View paramView, int paramInt, long paramLong)
      {
        MyFavoriteActivity.access$2(this.this$0).showContextMenu();
        MyFavoriteActivity.access$3(this.this$0, paramInt);
        return true;
      }
    };
    this.listContextMenuListener = new View.OnCreateContextMenuListener(this)
    {
      public void onCreateContextMenu(, View paramView, ContextMenu.ContextMenuInfo paramContextMenuInfo)
      {
        paramContextMenu.setHeaderTitle("请选择");
        paramContextMenu.add("删除");
      }
    };
    this.RestoreDialogClickListener = new DialogInterface.OnClickListener(this)
    {
      public void onClick(, int paramInt)
      {
        switch (paramInt)
        {
        default:
        case -1:
        }
        while (true)
        {
          paramDialogInterface.dismiss();
          return;
          this.this$0.startSycn(4, null);
        }
      }
    };
    this.BuckupDialogClickListener = new DialogInterface.OnClickListener(this)
    {
      public void onClick(, int paramInt)
      {
        switch (paramInt)
        {
        default:
        case -1:
        }
        while (true)
        {
          paramDialogInterface.dismiss();
          return;
          this.this$0.startSycn(5, null);
        }
      }
    };
    this.mTaskListener = new TaskAdapter(this)
    {
      protected ProgressDialog pd = null;

      public String getName()
      {
        return "WordTask";
      }

      public void onPostExecute(, TaskResult paramTaskResult)
      {
        if (paramTaskResult == TaskResult.OK)
          MyFavoriteActivity.access$5(this.this$0);
        while (true)
        {
          if (this.pd != null)
            this.pd.dismiss();
          return;
          MyFavoriteActivity.access$6(this.this$0, ((CommonTask.ProductFavoriteTask)paramGenericTask).getErrorMsg());
        }
      }

      public void onPreExecute()
      {
        switch (MyFavoriteActivity.access$4(this.this$0).getActionCode())
        {
        default:
          this.pd = ProgressDialog.show(MyFavoriteActivity.access$1(this.this$0), "", this.this$0.getString(2131361887), true);
        case 0:
        }
        while (true)
        {
          return;
          this.pd = ProgressDialog.show(MyFavoriteActivity.access$1(this.this$0), "", "正在加载数据，请稍候", true);
        }
      }

      public void onProgressUpdate(, Object paramObject)
      {
        if (MyFavoriteActivity.access$4(this.this$0).getActionCode() == 0)
        {
          MyFavoriteActivity.access$0(this.this$0).setQingBoList((List)paramObject);
          MyFavoriteActivity.access$0(this.this$0).notifyDataSetChanged();
        }
      }
    };
  }

  private boolean checkIsRegistered()
  {
    int i = 0;
    if (IKnow.mIKnowDataBase.getUser() == null)
      startActivity(new Intent(this, LoginActivity.class));
    while (true)
    {
      return i;
      i = 1;
    }
  }

  private void onSubmiteFinished()
  {
    if (this.mFavoriteTask.getActionCode() == 0);
    while (true)
    {
      return;
      if (this.mFavoriteTask.getActionCode() == 2)
        IKnow.mIKnowDataBase.removeFavoriteQingbo((QingBo)this.mFavoriteTask.getQBList().get(0));
      refresh();
      Toast.makeText(this.mContext, "同步完成", 0).show();
    }
  }

  private void onSubmitefailure(String paramString)
  {
    List localList = this.mFavoriteTask.getQBList();
    switch (this.mFavoriteTask.getActionCode())
    {
    default:
      refresh();
      Toast.makeText(this.mContext, paramString, 0).show();
    case 1:
    case 2:
    case 0:
    }
    while (true)
    {
      while (true)
      {
        while (true)
        {
          return;
          IKnow.mIKnowDataBase.addFavoriteQingbo((QingBo)localList.get(0));
          paramString = "已收藏";
        }
        IKnow.mIKnowDataBase.removeFavoriteQingbo((QingBo)localList.get(0));
        paramString = "已删除";
      }
      refresh();
    }
  }

  protected void dataMigration()
  {
    Cursor localCursor = IKnowDatabaseHelper.getDatabase(this.mContext).rawQuery("select count(*) as p from sqlite_master where type='table' and name='T_BD_PRODUCTFAVORITES'", null);
    int i = 0;
    if (localCursor.moveToNext())
      i = localCursor.getInt(0);
    localCursor.close();
    if (i == 0);
    while (true)
    {
      return;
      CommonTask.DataMigration localDataMigration = new CommonTask.DataMigration(this.mContext, this.mAdapter);
      Void[] arrayOfVoid = new Void[1];
      arrayOfVoid[0] = null;
      localDataMigration.execute(arrayOfVoid);
    }
  }

  public boolean onContextItemSelected(MenuItem paramMenuItem)
  {
    boolean bool;
    if (this.positionView < 0)
      bool = true;
    while (true)
    {
      while (true)
      {
        return bool;
        switch (paramMenuItem.getItemId())
        {
        default:
          bool = super.onContextItemSelected(paramMenuItem);
        case 0:
        }
      }
      QingBo localQingBo = this.mAdapter.getItem(this.positionView);
      ArrayList localArrayList = new ArrayList();
      localArrayList.add(localQingBo);
      startSycn(2, localArrayList);
      bool = true;
    }
  }

  protected void onCreate(Bundle paramBundle)
  {
    super.onCreate(paramBundle);
    requestWindowFeature(1);
    setContentView(2130903104);
    this.mContext = this;
    mfa = this;
    this.mList = ((MyListView)findViewById(2131296466));
    this.mAdapter = new FavoriteQingboAdapter(this.mContext, getLayoutInflater());
    this.mList.setAdapter(this.mAdapter);
    this.mList.setOnItemClickListener(this.listItemClickListener);
    this.mList.setOnItemLongClickListener(this.listItemLongClickListener);
    this.mList.setOnCreateContextMenuListener(this.listContextMenuListener);
    findViewById(2131296342).setOnClickListener(new View.OnClickListener(this)
    {
      public void onClick()
      {
        this.this$0.finish();
      }
    });
    findViewById(2131296344).setVisibility(8);
    startSycn(0, null);
  }

  public boolean onCreateOptionsMenu(Menu paramMenu)
  {
    getMenuInflater().inflate(2131492868, paramMenu);
    MenuItem localMenuItem1 = paramMenu.getItem(0);
    MenuItem localMenuItem2 = paramMenu.getItem(1);
    localMenuItem1.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener(this)
    {
      public boolean onMenuItemClick()
      {
        int i;
        if (!(IKnow.IsNetAviable()))
        {
          Toast.makeText(MyFavoriteActivity.access$1(this.this$0), MyFavoriteActivity.access$1(this.this$0).getString(2131361878), 0).show();
          i = 1;
        }
        while (true)
        {
          return i;
          if (MyFavoriteActivity.access$8(this.this$0))
            MsgDialog.showB2Dilog(MyFavoriteActivity.access$1(this.this$0), this.this$0.getString(2131361936), MyFavoriteActivity.access$9(this.this$0));
          i = 1;
        }
      }
    });
    localMenuItem2.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener(this)
    {
      public boolean onMenuItemClick()
      {
        int i;
        if (!(IKnow.IsNetAviable()))
        {
          Toast.makeText(MyFavoriteActivity.access$1(this.this$0), MyFavoriteActivity.access$1(this.this$0).getString(2131361878), 0).show();
          i = 1;
        }
        while (true)
        {
          return i;
          if (MyFavoriteActivity.access$8(this.this$0))
            MsgDialog.showB2Dilog(MyFavoriteActivity.access$1(this.this$0), this.this$0.getString(2131361935), MyFavoriteActivity.access$10(this.this$0));
          i = 1;
        }
      }
    });
    return true;
  }

  protected void onDestroy()
  {
    super.onDestroy();
  }

  protected void onPause()
  {
    if (this.mBinded)
    {
      unbindService(this.mSvrConn);
      this.mBinded = false;
    }
    super.onPause();
  }

  protected void onResume()
  {
    if (!(this.mBinded))
      this.mBinded = bindService(SERVICE_INTENT, this.mSvrConn, 1);
    super.onResume();
  }

  protected void onStart()
  {
    super.onStart();
  }

  protected void onStop()
  {
    super.onStop();
  }

  public void refresh()
  {
    this.mAdapter.setQingBoList(IKnow.mIKnowDataBase.getFavoriteQingbo(0, 999));
    this.mAdapter.notifyDataSetChanged();
  }

  public void startSycn(int paramInt, List<QingBo> paramList)
  {
    if ((this.mFavoriteTask != null) && (this.mFavoriteTask.getStatus() == AsyncTask.Status.RUNNING));
    while (true)
    {
      return;
      this.mFavoriteTask = new CommonTask.ProductFavoriteTask(this.mContext);
      this.mFavoriteTask.setListener(this.mTaskListener);
      if (paramList != null)
        this.mFavoriteTask.setQBList(paramList);
      TaskParams localTaskParams = new TaskParams();
      localTaskParams.put("action", Integer.valueOf(paramInt));
      localTaskParams.put("xmpp", this.mXmppFacade);
      CommonTask.ProductFavoriteTask localProductFavoriteTask = this.mFavoriteTask;
      TaskParams[] arrayOfTaskParams = new TaskParams[1];
      arrayOfTaskParams[0] = localTaskParams;
      localProductFavoriteTask.execute(arrayOfTaskParams);
    }
  }

  private class XmppServiceConnection
  implements ServiceConnection
  {
    public void onServiceConnected(, IBinder paramIBinder)
    {
      MyFavoriteActivity.access$7(this.this$0, IXmppFacade.Stub.asInterface(paramIBinder));
    }

    public void onServiceDisconnected()
    {
      MyFavoriteActivity.access$7(this.this$0, null);
    }
  }
}